package br.com.passeionaweb.appengine.tabelafipe.data;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Model {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	public Long getMakeid() {
		return makeid;
	}

	public void setMakeid(Long makeid) {
		this.makeid = makeid;
	}

	public Long getId() {
		return id;
	}

	public static String KEY_MODEL = "model";

	@Persistent
	private String model;

	@Persistent
	private Long makeid;

	public Model(String model, Long makeid) {
		super();
		this.model = model;
		this.makeid = makeid;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public static boolean exists(String model) {
		return getModel(model) != null;
	}

	public static Model getModel(String modelname) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Model.class);
		q.setFilter(KEY_MODEL + " == '" + modelname+"'");
		List<Model> models = (List<Model>) q.execute();
		if (models.isEmpty()) {
			return null;
		} else {
			return models.get(0);
		}
	}

}
